package kuang.juc.forkjoin;

import java.util.concurrent.RecursiveTask;

/**
 * 如何使用ForkJoin
 * 1. 通过ForkJoinPool执行
 * 2. 计算任务ForkJoinPool.execute(ForkJoinTask task)
 * 3. 计算类要继承ForkJoinTask
 */
public class ForkJoinDemo extends RecursiveTask<Long> {
    private Long start;
    private Long end;
    private Long temp=10000L;

    public ForkJoinDemo(Long start, Long end) {
        this.start = start;
        this.end = end;
    }

    @Override
    protected Long compute() {
        if ((end-start)<temp){
            Long sum=0L;
            for (Long i = start; i < end; i++) {
                sum+=i;
            }
            return sum;
        }else { //forkjoin方式
            Long middle=(end+start)/2;
            ForkJoinDemo task1 = new ForkJoinDemo(start, middle);
            task1.fork();   //拆分任务，把任务压入线程队列
            ForkJoinDemo task2 = new ForkJoinDemo(middle, end);
            task2.fork();
            //取值合并
            return task1.join()+task2.join();

        }
    }
}
